package com.panopset.java;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * Provides a universal version number, separate from the individual projects
 * maven versions, which may, or may not change with a particular release.
 *
 * FormatSource changes the source code of this class so that the version
 * matches the date stamp of the day that FormatSource is run.
 *
 * @author Karl Dinwiddie
 *
 */
public final class UniversalVersion {

    /**
     * This String is changed by FormatSource to match yyyyMMdd formatted
     * current date.
     */
    private static final String UNI_VERSION = "20091101";

    /**
     * Returns Panopset universal version number.
     *
     * @return yyyyMMdd formatted date as universal version number.
     */
    @Override
    public String toString() {
        return UNI_VERSION;
    }

    /**
     * yyyyMMdd.
     */
    public static final String SIMPLE_DATE_FORMAT = "yyyyMMdd";

    /**
     * yyyyMMdd.
     */
    public static final SimpleDateFormat VERSION_FORMAT = new SimpleDateFormat(
            SIMPLE_DATE_FORMAT);

    /**
     * @return Universal version, last creation date formatted as yyyyMMdd.
     */
    public static String getVersion() {
        return asUniversalVersion().toString();
    }
    /**
     * Singleton pattern static instance.
     */
    private static UniversalVersion singleton;

    /**
     * Singleton pattern static instance accessor.
     *
     * @return UniversalVersion singleton static instance.
     */
    public static UniversalVersion asUniversalVersion() {
        if (singleton == null) {
            singleton = new UniversalVersion();
        }
        return singleton;
    }

    /**
     * Prevent external instantiation.
     */
    private UniversalVersion() {
        // nothing to do here.
    }

    /**
     * Called by FormatSource to update the run time version.
     *
     * @return New yyyyMMdd formatted date stamp.
     */
    String createNewVersion() {
        return VERSION_FORMAT.format(new Date());
    }
}
